package com.xj.hk.controller;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xj.hk.entity.Ck;
import com.xj.hk.entity.Sp;
import com.xj.hk.service.CkService;
import com.xj.hk.service.SpService;
import com.xj.util.JsonUtil;
import com.xj.util.Page;

/**
 * 前台业务处理类
 * @author AutoExport
 *
 */
@Controller
@RequestMapping(value="/")
public class FrontController{
	
	private final String returnStr="/";
	
	@Autowired
	private SpService spService;
	public void setSpService(SpService spService) {
		this.spService = spService;
	}
	
	@Autowired
	private CkService ckService;
	public void setCkService(CkService ckService) {
		this.ckService = ckService;
	}
		
	/**
	 * 跳转首页页面
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/index.htm")
	public ModelAndView index(HttpServletRequest request,HttpSession session){
		Map<String, Object> map = new HashMap<String, Object>();
		return new ModelAndView(returnStr+"index", map);
	}
	
	/**
	 * 跳转窗口设置页面
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/ck.htm")
	public ModelAndView ck(HttpServletRequest request,HttpSession session){
		Map<String, Object> map = new HashMap<String, Object>();
		return new ModelAndView(returnStr+"ck", map);
	}
	
	/**
	 * 跳转视频上传页面
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/sp.htm")
	public ModelAndView sp(HttpServletRequest request,HttpSession session){
		Map<String, Object> map = new HashMap<String, Object>();
		return new ModelAndView(returnStr+"sp", map);
	}
	
	/**
	 * 跳转视频上传页面
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/addCk.htm")
	public ModelAndView addCk(HttpServletRequest request,HttpSession session){
		Map<String, Object> map = new HashMap<String, Object>();
		return new ModelAndView(returnStr+"add", map);
	}
	
	/**
	 * 窗口分页	
	 * @param request
	 * @param response
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/listJson.htm")
	@ResponseBody
	public String listJson(HttpServletRequest request, HttpServletResponse response,HttpSession session){	
		Map<String,Object> map= new HashMap<String, Object>();
		//分页
		String start    =request.getParameter("page");		//从多少开始
		String length =request.getParameter("limit");		//数据长度
		
		String orderColumn    =request.getParameter("orderColumn");		//从多少开始
		String orderDir 			   =request.getParameter("orderDir");		//数据长度
       
        Page<Object[]> list = ckService.queryByPage(map,Integer.parseInt(start), Integer.parseInt(length), orderColumn, orderDir);
             
        Map<String,Object> resmap = new HashMap<String, Object>();
        resmap.put("code", 0);
        resmap.put("msg","");
        resmap.put("count", list.getTotalCount());
        resmap.put("data", list.getResult());
		return JsonUtil.javaToJsonStr(resmap);
	}
	
	/**
	 * 窗口分页	
	 * @param request
	 * @param response
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/listSp.htm")
	@ResponseBody
	public String listSp(HttpServletRequest request, HttpServletResponse response,HttpSession session){	
		Map<String,Object> map= new HashMap<String, Object>();
		//分页
		String start    =request.getParameter("page");		//从多少开始
		String length =request.getParameter("limit");		//数据长度
		
		String orderBy  ="addtime";	
		String order		="desc";	
		
		map.put("type", 1);
       
        Page<Sp> list =spService.queryByPage(map, Integer.parseInt(start), Integer.parseInt(length), orderBy, order);
             
        Map<String,Object> resmap = new HashMap<String, Object>();
        resmap.put("code", 0);
        resmap.put("msg","");
        resmap.put("count", list.getTotalCount());
        resmap.put("data", list.getResult());
		return JsonUtil.javaToJsonStr(resmap);
	}
	
	/**
	 * 增加窗口
	 * @param ck
	 * @return
	 */
	@RequestMapping(value="/add.htm")
	@ResponseBody
	public String add(Ck ck){	
		ckService.save(ck);
		return "success";
	}
	
	/**
	 * 删除文件
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/delete.htm")
	@ResponseBody
	public String delete(int id){	
		spService.delete(id);
		return "success";
	}
	
	/**
	 * 修改路径
	 * @param id
	 * @param sid
	 * @return
	 */
	@RequestMapping(value="/update.htm")
	@ResponseBody
	public String update(int id,int sid){	
		ckService.update(id, sid);
		return "success";
	}
		
	/**
	 *  跳转文章页面
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/wz.htm")
	public ModelAndView wz(HttpServletRequest request,HttpSession session){
		Map<String, Object> map = new HashMap<String, Object>();
		return new ModelAndView(returnStr+"wz", map);
	}
	
	/**
	 * 新增文章
	 * @param wz
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/addWz.htm")
	public ModelAndView addWz(Sp sp,HttpServletRequest request,HttpSession session){
		sp.setType(2);
		spService.save(sp);		
		return new ModelAndView("redirect:/wzList.htm");	
	}
	
	/**
	 * 文章分页
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listWz.htm")
	@ResponseBody
	public String listWz(HttpServletRequest request){
		Map<String,Object> map= new HashMap<String, Object>();
		//分页
		String start    =request.getParameter("page");		//从多少开始
		String length =request.getParameter("limit");		//数据长度
		
		String orderBy  ="addtime";	
		String order		="desc";	
		
		map.put("type", 2);
       
        Page<Sp> list =spService.queryByPage(map, Integer.parseInt(start), Integer.parseInt(length), orderBy, order);
             
        Map<String,Object> resmap = new HashMap<String, Object>();
        resmap.put("code", 0);
        resmap.put("msg","");
        resmap.put("count", list.getTotalCount());
        resmap.put("data", list.getResult());
    	return JsonUtil.javaToJsonStr(resmap);
	}
	
	/**
	 * 跳转文章详情页面
	 * @param id
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/wzList.htm")
	public ModelAndView wzList(HttpServletRequest request,HttpSession session){
		Map<String, Object> map = new HashMap<String, Object>();
		return new ModelAndView(returnStr+"wzList", map);
	}
	
	/**
	 * 跳转文章详情页面
	 * @param id
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/wzDetail.htm")
	public ModelAndView wzDetail(int id,HttpServletRequest request,HttpSession session){
		Map<String, Object> map = new HashMap<String, Object>();
		Sp sp=spService.getEntity(id);		
		map.put("wz", sp);
		return new ModelAndView(returnStr+"wzDetail", map);
	}
	
	@RequestMapping(value="/welcome.htm")
	public ModelAndView welcome(HttpServletRequest request,HttpSession session){
		Map<String, Object> map = new HashMap<String, Object>();
		return new ModelAndView(returnStr+"welcome", map);
	}
	
}
